<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Crypt;

class UserController extends Controller
{
    //add
    public function add()
    {
    	return view('admin.user.add');
    }

    //insert
    public function insert(Request $request)
    {
    	$this -> validate($request, [
    		'password' => 'required',
    		'repassword' => 'required|same:password',
    		'email' => 'required|email',
    		'phone' => 'required|max:11|min:11'],[
    		'password.required' => '密码不能为空',
    		'repassword.required' => '请重复输入密码',
    		'repassword.same' => '两次密码不一致',
    		'email.required' => '请输入邮箱',
    		'email.email' => '邮箱格式不正确',
    		'phone.required' => '请输入手机号',
    		'phone.max' => '请输入正确的手机号',
    		'phone.min' => '请输入正确的手机号'
    		]);
    	//处理所有数据
    	$data = $request -> except('_token', 'repassword');
        $data['ctime'] = time();
    	$data['password'] = Crypt::encrypt($data['password']);

    	$data['remember_token'] = str_random(70);
    	$time = date("Y-m-d H:i:s");
    	$data['created_at'] = $time;
    	$data['updated_at'] = $time;

    	//添加数据
    	$res = DB::table('users') -> insert($data);

        if($res)
        {

            $sele = DB::table('users') -> where('email', $data['email']) -> first();

            $resd = DB::table('userdetail') -> insert(['id' => $sele -> id, 'name' => $sele -> email]);
            
            if($resd)
            {
                return redirect('admin/user/index') -> with(['info' => '添加成功']);
            }else
            {
                return redirect('admin/user/add') -> with(['info' => '添加失败']);
            }

        }else
        {
            return redirect('admin/user/add') -> with(['info' => '添加失败']);
        }

    	
    }
    //index
    public function index(Request $request)
    {

    	$num = $request -> input('num', 18);


    	$data = DB::table('users')
            -> join('userdetail', 'users.id', '=', 'userdetail.id')
            -> select('users.*', 'userdetail.name')
    		-> where(function($query) use ($request){
    			$query -> where('email', 'like', '%'.$request -> input('keyword').'%');
    		})
    		-> paginate($num);
        return view('admin.user.index', ['data' => $data, 'request' => $request -> all()]);
    }

    //edit
    public function edit($id)
    {
    	$data = DB::table('users') -> where('id', $id) -> first();
    	

    	return view('admin.user.edit', ['data' => $data]);
    }

    //update
    public function update(Request $request)
    {
    	$this -> validate($request, [
    		'email.required' => '邮箱不能为空',
    		'email.email' => '邮箱格式不正确',
    		'phone.required' => '请输入手机号',
    		'phone.max' => '请输入正确的手机号',
    		'phone.min' => '请输入正确的手机号'
    		]);

    	$data = $request -> except('_token', 'id');

    	$data['updated_at'] = date('Y-m-d H:i:s');

    	$res = DB::table('users') -> where('id', $request -> input('id')) -> update($data);

    	if($res)
    	{
    		return redirect('/admin/user/index') -> with(['info' => '更新成功']);
    	}else
    	{
    		return back() -> with(['info' => '更新失败']);
    	}

    }

    //delete
    public function delete($id)
    {
        $res = DB::table('post') -> where('uid', $id) -> delete();
        $res = DB::table('reply') -> where('uid', $id) -> delete();

    	$res = DB::table('users') -> where('id', $id) -> delete();
    	if($res)
    	{
            $rew = DB::table('userdetail') -> where('id', $id) -> delete();
            if($res)
            {
    		  return redirect('/admin/user/index') -> with(['info' => '删除成功']);
            }else
            {
                return back() -> with(['info' => '删除失败']);
            }
    	}else
    	{
    		return back() -> with(['info' => '删除失败']);
    	}
    }

    //ajaxUpdate
    public function ajaxUpdate(Request $request)
    {
    	$id = $request -> input('id');

    	$data = DB::table('users') -> where('id', $id) -> first();
    	if($data -> status == '0')
    	{
    		$res = DB::table('users') ->  where('id', $id) -> update(['status' => '1']);
    		if($res)
    		{
    			return response() -> json(0);
    		}else
    		{
    			return response() -> json(1);
    		}

    	}else
    	{
    		$res = DB::table('users') -> where('id', $id) -> update(['status' => '0']);
    		if($res)
    		{
    			return response() -> json(2);
    		}else
    		{
    			return response() -> json(3);
    		}
    	}

    }

    public function ajaxEmail(Request $request)
    {
    	$email = $request -> input('email');
    	//查询
    	$res = DB::table('users') -> where('email', $email) -> first();
    	if($res)
    	{
    		//1代表用户名已经存在
    		return response() -> json(1);
    	}else
    	{

    		if(empty($email))
    		{
    			return response() -> json(2);
    		}else
    		{
	    		$res = DB::table('users') -> where('id', $request -> input('id')) -> update(['email' => $email]);
	    		if($res)
	    		{
	    			return response() -> json(0);
	    		}else
	    		{
	    			return response() -> json(2);
	    		}
	    	}
    	}

    }
}
